package tp.badugi.bot.guiViewsBot;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import tp.badugi.bot.GuiClientWithBot;
import tp.badugi.net.GameStatus;

public class GameViewBot extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5322476492077108614L;

	private GuiClientWithBot client;

	private final Dimension gameViewSize = new Dimension(800, 600);
	
	private int gamePlayerCardWidth;
	private int gamePlayerCardHeight;
	
	private String[] gamePlayerInfoTableColumnNames = new String[]{"Player name","Chips","Chips in pot","Connected"};
	
	public JButton gameLeaveTable = new JButton("Leave table");
	public JButton gameDisconnect = new JButton("Disconnect");
	public JButton gameBotOn = new JButton("Bot OFF");
	private JButton gamePlayerCardButton[] = new JButton[4];
	
	private JPanel gameConsolePlayerButtonPanel = new JPanel();
	private JPanel gameGraphicalPanel;
	private JPanel gamePlayerInfoPanel = new JPanel();
	private JPanel gamePlayerCardPanel = new JPanel();
	private JPanel gameStatusInfoPanel = new JPanel();
	private JPanel gameButtonPanel = new JPanel();
	
	private JLabel gameTableNameAndServerLabel = new JLabel("",SwingConstants.CENTER);
	private JLabel gamePlayerNameLabel = new JLabel();
	private JLabel gamePlayerChipsLabel  = new JLabel();
	
	private GameViewPlayerInfoTableBot gamePlayerInfoTable;
	
	private JScrollPane gameConsoleScrollPane  = new JScrollPane(null,
			ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
	
	private JTextArea gameStatusTextArea = new JTextArea();
	public JTextArea gameConsoleTextArea = new JTextArea();
	
	public GameViewBot(GuiClientWithBot client, GameStatus gameStatus, String serverIpAdress,
			int serverPortNumber) {
		
		this.client = client;
		
		this.gameGraphicalPanel = new GamePanelBot(client);
		
		this.gamePlayerInfoTable = new GameViewPlayerInfoTableBot(
				this.gamePlayerInfoTableColumnNames,
				this.gamePlayerInfoTableColumnNames.length,
				6);
		
		this.makeView();
		this.refreshView(gameStatus, serverIpAdress, serverPortNumber);
	}
	
	private void makeView(){
		
		this.setTitle(this.client.title);
		
		this.setBounds(new java.awt.Rectangle(this.client.windowBoundPoint, this.gameViewSize));
		
		this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
		
		//game status
		this.gameStatusTextArea.setEditable(false);
		this.gameStatusTextArea.setPreferredSize(new Dimension(200,50));
		
		
		this.gameStatusInfoPanel.setLayout(new BoxLayout(this.gameStatusInfoPanel, BoxLayout.X_AXIS));
		this.gameStatusInfoPanel.setPreferredSize(new Dimension(600,50));
		this.gameStatusInfoPanel.add(this.gameStatusTextArea);
		this.gameStatusInfoPanel.add(this.gamePlayerInfoTable);
		//game status
		
		
		//console + myinfo + buttons
		
		this.gameConsoleTextArea.setEditable(false);
		this.gameConsoleTextArea.setLineWrap(true);
		
		this.gameConsoleScrollPane.setViewportView(this.gameConsoleTextArea);
		this.gameConsoleScrollPane.setPreferredSize(new Dimension(350,100));
		
		//myinfo
		this.gamePlayerCardPanel.setPreferredSize(new Dimension(200,100));
		this.gamePlayerCardPanel.setLayout(new GridLayout(1,4));
		
		this.gamePlayerInfoPanel.setPreferredSize(new Dimension(400,100));
		this.gamePlayerInfoPanel.setLayout(new BoxLayout(this.gamePlayerInfoPanel, BoxLayout.X_AXIS));
		this.gamePlayerInfoPanel.add(this.gamePlayerNameLabel);
		this.gamePlayerInfoPanel.add(Box.createHorizontalGlue());
		this.gamePlayerInfoPanel.add(this.gamePlayerCardPanel);
		this.gamePlayerInfoPanel.add(Box.createHorizontalGlue());
		this.gamePlayerInfoPanel.add(this.gamePlayerChipsLabel);
		this.gamePlayerInfoPanel.setAlignmentY(java.awt.Component.CENTER_ALIGNMENT);
		
		this.gamePlayerNameLabel.setBorder(new EmptyBorder(0,10,0,0));
		
		for (int i = 0; i < 4; i++){
			this.gamePlayerCardPanel.add(this.gamePlayerCardButton[i] = new JButton());
			this.gamePlayerCardButton[i].setEnabled(false);
		}
		
		this.gamePlayerChipsLabel.setBorder(new EmptyBorder(0,0,0,10));
		
		
		//myinfo
		
		//button
		this.gameDisconnect.addActionListener(this.client);
		this.gameLeaveTable.addActionListener(this.client);
		this.gameBotOn.addActionListener(this.client);
		
		this.gameButtonPanel.setLayout(new GridLayout(3,1));
		this.gameButtonPanel.add(this.gameLeaveTable);
		this.gameButtonPanel.add(this.gameDisconnect);
		this.gameButtonPanel.add(this.gameBotOn);
		//button
		
		this.gameConsolePlayerButtonPanel.setLayout(new BoxLayout(this.gameConsolePlayerButtonPanel, BoxLayout.X_AXIS));
		
		this.gameConsolePlayerButtonPanel.add(this.gameConsoleScrollPane);
		this.gameConsolePlayerButtonPanel.add(this.gamePlayerInfoPanel);
		this.gameConsolePlayerButtonPanel.add(this.gameButtonPanel);
		//console + myinfo + buttons
		
		
		this.add(this.gameTableNameAndServerLabel);
		this.add(this.gameGraphicalPanel);
		this.add(this.gameStatusInfoPanel);
		this.add(this.gameConsolePlayerButtonPanel);
		
		this.addWindowListener(this.client.windowAdapter);
		
		this.setVisible(true);
		this.setResizable(false);
		
		this.gamePlayerCardWidth = this.gamePlayerCardButton[0].getWidth();
		this.gamePlayerCardHeight = this.gamePlayerCardButton[0].getHeight();
	}
	
	public void refreshView(GameStatus gameStatus, String serverIpAdress,
			int serverPortNumber){
		
		Image image;
		Image scaledImage;
		ImageIcon scaledImageIcon;
		
		// -- Table title
		
		this.gameTableNameAndServerLabel.setText(gameStatus.tableName +
				" on " + serverIpAdress + ":" + serverPortNumber);
		
		// -- My status
		
		this.gamePlayerNameLabel.setText(gameStatus.playerNames[gameStatus.myPlayerNumber]);
		
		if (gameStatus.myCards != null){
			for (int i = 0; i < 4; i++){
				image = this.client.imageProvider.getCardImage(gameStatus.myCards[i]);
				scaledImage = image.getScaledInstance(this.gamePlayerCardWidth, this.gamePlayerCardHeight, Image.SCALE_SMOOTH);
				scaledImageIcon = new ImageIcon(scaledImage);
				this.gamePlayerCardButton[i].setIcon(scaledImageIcon);
				this.gamePlayerCardButton[i].setDisabledIcon(scaledImageIcon);
			}
		}
		
		this.gamePlayerChipsLabel.setText(String.valueOf(gameStatus.myChipCount));
		
		//-- Game status
		
		this.gameStatusTextArea.setText("");
		
		this.gameStatusTextArea.append("Betting round " + String.valueOf(gameStatus.bettingRound) + "\n");
		this.gameStatusTextArea.append("Current bet " + String.valueOf(gameStatus.betAmount) + "\n");
		this.gameStatusTextArea.append("Pot size " + String.valueOf(gameStatus.potSize) + "\n");
		this.gameStatusTextArea.append("Small blind " + String.valueOf(gameStatus.smallBlind) + "\n");
		this.gameStatusTextArea.append("Big blind " + String.valueOf(gameStatus.bigBlind) + "\n");
		this.gameStatusTextArea.append("Number of players " + String.valueOf(gameStatus.numberOfPlayers) + "\n");
		this.gameStatusTextArea.append("Dealer " + gameStatus.playerNames[gameStatus.playerWithDealerButton] + "\n");
		
		if (gameStatus.playerInAction < 0){
			this.gameStatusTextArea.append("Player in action none \n");
		}
		else{
			this.gameStatusTextArea.append("Player in action " + gameStatus.playerNames[gameStatus.playerInAction] + "\n");
		}
		
		//names
		for(int j = 0; j < gameStatus.numberOfPlayers; j++){
			this.gamePlayerInfoTable.dataLabels[0][j].setText(
									gameStatus.playerNames[j]);
		}
		
		//chip counts
		for(int j = 0; j < gameStatus.numberOfPlayers; j++){
			this.gamePlayerInfoTable.dataLabels[1][j].setText(
									String.valueOf(gameStatus.playerChipCounts[j]));
		}
		
		//chip contribution
		for(int j = 0; j < gameStatus.numberOfPlayers; j++){
			this.gamePlayerInfoTable.dataLabels[2][j].setText(
									String.valueOf(gameStatus.playerPotContributions[j]));
		}
		
		//connection statuses
		for(int j = 0; j < gameStatus.numberOfPlayers; j++){
			this.gamePlayerInfoTable.dataLabels[3][j].setText(
									String.valueOf(gameStatus.playerStatuses[j]));
		}
		
		// -- Bot Button
		
		if(this.client.botOn){
			this.gameBotOn.setBackground(Color.GREEN);
			this.gameBotOn.setText("Bot ON");
		}
		else{
			this.gameBotOn.setBackground(this.client.defaultButtonColor);
			this.gameBotOn.setText("Bot OFF");
		}
		
		this.setVisible(true);
	}
}
